package com.klhappy.houyuan.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.GetGoodsBean;
import com.klhappy.houyuan.bean.OnSelfGoodBean.OneSelfGoodBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.view.ImageManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2015/11/13.
 */
public class ShoppingDialog implements CallBackInterModle {

    private Dialog dialog;
    private Context context;
    private int amount = 1;
    private OneSelfGoodBean goodBean;
    private GetGoodsBean goodsBean;
    private HouYuanModle modle;

    @InjectView(R.id.back_rl) RelativeLayout back_rl;
    @InjectView(R.id.back) ImageView back;
    @InjectView(R.id.down) ImageView down;
    @InjectView(R.id.number) TextView num;
    @InjectView(R.id.up) ImageView up;
    @InjectView(R.id.header_img) ImageView header_img;
    @InjectView(R.id.unit_price) TextView unit_price;
    @InjectView(R.id.tv_unit) TextView tv_unit;
    @OnClick(R.id.certain)
    public void certain(){
        setJson();
    }
    @OnClick(R.id.back)
    public void back(){
        Dismiss();
    }
    @OnClick(R.id.down)
    public void down(){
        if (amount > 1){
            amount --;
            num.setText("" + amount);
        }
    }
    @OnClick(R.id.up)
    public void up(){
        amount ++;
        num.setText("" + amount);
    }

    public ShoppingDialog(Context context ,OneSelfGoodBean goodBean) {
        this.context = context;
        this.goodBean = goodBean;
        View view = LayoutInflater.from(context).inflate(R.layout.shopping_dialog ,null);
        ButterKnife.inject(this ,view);
        modle = new HouYuanModle(context);
        modle.setCallBackInterModle(this);

        intView();

        dialog = new Dialog(context , R.style.Dialog_Fullscreen);
        dialog.setContentView(view);
    }

    private void intView(){
        if (goodBean.getG_icon() != null){
            ImageManager.loadImage(UrlConstant.BASE_URL + goodBean.getG_icon(), header_img);
            unit_price.setText("￥" + goodBean.getG_price());
            tv_unit.setText(goodBean.getG_unit());
        }
    }

    private void setJson(){
        goodsBean = new GetGoodsBean();
        goodsBean.setId(goodBean.getG_id() + "");
        goodsBean.setNo(num.getText().toString());

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            object.put("id" ,goodsBean.getId() + "");
            object.put("no" ,goodsBean.getNo());
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject Nobject = new JSONObject();
        try {
            Nobject.put("goods" ,array);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        modle.setOrder(Nobject);
    }

    public void show(){
        dialog.show();
    }

    public void Dismiss(){
        if (dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.ADDSHOP_URL)){
            if (o.getCode() == 200){
                Toast.makeText(context ,o.getMsg() ,Toast.LENGTH_SHORT).show();
                Dismiss();
            }else {
                Toast.makeText(context ,o.getMsg() ,Toast.LENGTH_SHORT).show();
            }
        }
    }
}
